package com.web.servlet;

import com.dao.impl.GetPhoneNumber;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @Author wangzhipeng
 * @Date 2020-08-31 2:08 下午
 * @Description
 */
@WebServlet(urlPatterns = "/SendPhoneNumServlet")
public class SendPhoneNumServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String phoneNumber = request.getParameter("phone");

        Map<String, Object> map = GetPhoneNumber.toSendPhone(phoneNumber);
        response.setCharacterEncoding("UTF_8");
        response.setContentType("application/json;charset=UTF-8");

        String code = (String) map.get("code");
        String phoneCode = null;
        PrintWriter printWriter = response.getWriter();
        ObjectMapper objectMapper = new ObjectMapper();
        if(code.equals("0000")){
            phoneCode = map.get("phoneCode")+"";
            String s = objectMapper.writeValueAsString(map);
            printWriter.println(s);
        }else {
            phoneCode = (String) map.get("phoneCode")+"";
            String s = objectMapper.writeValueAsString(map);
            printWriter.println(s);
        }
        printWriter.flush();
        printWriter.close();


    }
}
